﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Data;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;

namespace University.Workshop.Presenters
{
    public class PresenterReportSale
    {
        private IReportSale Report;
        public PresenterReportSale(IReportSale report)
        {
            Report = report;  
        }
        public bool ReportState(){
            Report.VisibleProduct = Report.VisibleProduct;
            Report.VisibleClient = Report.VisibleClient;
            if (!Report.VisibleClient) {
                Report.ClientId = string.Empty;
            }
            if (!Report.VisibleProduct) { 
                Report.ProductId = string.Empty;
            }
            return true;
        }
        public bool SetProduct()
        {
            Report.ProductId = Report.VariablesProduct;
            return true;
        }

        public bool SetClient()
        {
            Report.ClientId = Report.VariablesClient;
            return true;
        }

        public string GenerateReport()
        {
            DataTable workTable = Report.DataReport;
            Document document = new Document(PageSize.LETTER, 50, 40, 30, 30);
            FileStream fileDocument = new FileStream("ReporteDeVentas.pdf", FileMode.OpenOrCreate);
            PdfWriter.GetInstance(document, fileDocument);
            document.Open();
            document.Add(new Paragraph(new Chunk("Reportes Productos Vendidos - Farmacias 'La Aspirina Feliz'", FontFactory.GetFont(FontFactory.TIMES_BOLD, 18, Font.UNDERLINE))));
            document.Add(new Paragraph(new Chunk("Generado por: " + Report.UserId + "\t Fecha:" + DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.TIMES, 12, Font.NORMAL, BaseColor.DARK_GRAY))));
            string header ="";
            if (!(string.IsNullOrEmpty(Report.ProductId)) && !(string.IsNullOrEmpty(Report.ClientId)))
            {
                header += "Filtros";
                if (!(string.IsNullOrEmpty(Report.ProductId)))
                {
                    header += "        Cod. Producto: " + Report.ProductReport;
                }
                if (!(string.IsNullOrEmpty(Report.ClientId)))
                {
                    header += "       Cliente:" + Report.ClientReport;
                }
            }
            document.Add(new Paragraph(new Chunk(header, FontFactory.GetFont(FontFactory.TIMES, 12, Font.NORMAL))));
            PdfPTable table = new PdfPTable(workTable.Columns.Count);
            PdfPCell cell = new PdfPCell();
            document.Add(new Paragraph("\n"));
            cell.HorizontalAlignment = 1;
            iTextSharp.text.Font titulos = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
            foreach (DataColumn dc in workTable.Columns)
            {
                table.AddCell(new Phrase(dc.ColumnName, titulos));
            }
            foreach (DataRow d in workTable.Rows)
            {
                foreach (object ob in d.ItemArray)
                {
                    table.AddCell(ob.ToString());
                }

            }
            document.Add(table);
            document.Close();
            if (document != null) {
                document.Dispose();
            }
            return "ReporteDeVentas.pdf";
        }

        public bool SearchReport()
        {

            DataTable resp= Business.ReportProduct.SearchReport(Report.DateInit, Report.DateEnd, Report.ClientId, Report.ProductId);
            if (resp.Rows.Count == 0) {
                Report.VisibilityReportViewer = false;
                throw new BusinessException("No existen datos para presentarse en el Reporte");
            }
            Report.VisibilityReportViewer = true;
            Report.DataReport = resp;
            Report.ClientReport = Report.ClientId;
            Report.ProductReport = Report.ProductId;
            Report.VisibilityReportViewer = true;
            return true;
        }
    }
}
